package com.hotchpotch.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

/**
 * Created by Administrator on 2017/5/11 0011.
 */

public abstract class BaseFragment extends Fragment {
    //获取布局文件ID
    protected abstract int getLayoutId();

    protected BaseActivity mActivity;

    //布局加载
    protected abstract void initView(View view);

    //监听事件
    protected abstract void initEvents();

    //数据
    protected abstract void initData();

    private boolean isVisible = false;//当前Fragment是否可见
    private boolean isInitView = false;//是否与View建立起映射关系
    private boolean isFirstLoad = true;//是否是第一次加载数据

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setRetainInstance(true);
        View view = inflater.inflate(getLayoutId(), container, false);
        initView(view);
        isInitView = true;
        lazyLoadData();
        return view;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (BaseActivity) context;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser) {
            isVisible = true;
            lazyLoadData();
        } else {
            isVisible = false;
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    private void lazyLoadData() {
        if (isFirstLoad) {
//            LogUtil.m("第一次加载 " + " isInitView  " + isInitView + "  isVisible  " + isVisible + "   " + this.getClass().getSimpleName());
        } else {
//            LogUtil.m("不是第一次加载" + " isInitView  " + isInitView + "  isVisible  " + isVisible + "   " + this.getClass().getSimpleName());
        }
        if (!isFirstLoad || !isVisible || !isInitView) {
//            LogUtil.m("不加载" + "   " + this.getClass().getSimpleName());
            return;
        }

//        LogUtil.m("完成数据第一次加载" + "   " + this.getClass().getSimpleName());
//        Log.e(getActivity().getClass().getSimpleName(), "完成数据第一次加载");
        initData();
        isFirstLoad = false;
    }

    /**
     * 获取标题栏的头像
     */
    /*protected ImageView getIcon(View view) {
        ImageView imageView = (ImageView) view.findViewById(R.id.user_icon);
        return imageView;
    }*/

    /**
     * 标题栏头像点击
     */
    /*protected ImageView setIconClick(View view) {
        ImageView imageView = (ImageView) view.findViewById(R.id.user_icon);
        return imageView;
    }*/

    /**
     * 标题栏购物车点击
     */
    /*protected RelativeLayout setCartClick(View view) {
        RelativeLayout layoutCart = (RelativeLayout) view.findViewById(R.id.titlebar_cart);
        return layoutCart;
    }*/

    /**
     * 标题栏更多点击
     */
    /*protected RelativeLayout setMoreClick(View view) {
        RelativeLayout layoutMore = (RelativeLayout) view.findViewById(R.id.titlebar_more);
        return layoutMore;
    }*/

    /**
     * activity跳转
     *
     * @param from
     * @param to
     * @param data 携带数据
     */
    protected void goToPage(Context from, Class<? extends BaseActivity> to, Bundle data) {
        Intent i = new Intent();
        i.setClass(from, to);
        if (data != null) {
            i.putExtras(data);
        }
        from.startActivity(i);
    }

    /**
     * 提示内容
     *
     * @param msg
     */
    protected void showToast(String msg) {
        if (!msg.isEmpty() && getActivity() != null) {
            Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 显示 Snackbar（无action）
     * @param v
     * @param msg
     */
    protected void showSnackbar(View v, String msg) {
        Snackbar.make(v, msg, Snackbar.LENGTH_SHORT).show();
    }

    /**
     * 显示 Snackbar（有action）
     * @param v
     * @param msg
     * @param action
     */
    protected void showSnackbar(View v, String msg, String action) {
        Snackbar.make(v, msg, Snackbar.LENGTH_SHORT)
                .setAction(action, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                }).show();
    }

    /**
     * 提示网络连接错误
     */
    protected void showNetErrorToast() {
        showToast("网络连接错误，请检查网络后重试");
    }

    /**
     * 显示标题栏选项框
     */
    /*protected void showTitlePop(View view) {
        if (view != null) {
            titlePopup.show(view);
        }
    }*/

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean("STATE_SAVE_IS_HIDDEN", isHidden());
    }
}
